# -*- coding: utf-8 -*-
#
# File: formulario.py
#
# Copyright (c) 2007 by CTEC
# Generator: ArchGenXML Version 1.5.0
#            http://plone.org/products/archgenxml
#
# GNU General Public License (GPL)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

__author__ = """CTEC <joaojoao@gmail.com>"""
__docformat__ = 'plaintext'

from AccessControl import ClassSecurityInfo
from Products.Archetypes.atapi import *
from Products.Archetypes.SQLStorage import *
from Products.ibictn.config import *

# additional imports from tagged value 'import'
import DateTime

##code-section module-header #fill in your manual code here
##/code-section module-header

schema = Schema((

    DateTimeField(
        name='data',
        default=DateTime.DateTime(),
        widget=CalendarWidget(
            label="Data de criação",
            description="data da solicitação",
            format="%d/%m/%Y",
            date_only="1",
            show_hm=False,
            label_msgid='ibictn_label_data',
            description_msgid='ibictn_help_data',
            i18n_domain='ibictn',
        ),
        mode="r"
    ),

    StringField(
        name='departamento',
        widget=StringWidget(
            label="Área Solicitante",
            description="",
            label_msgid='ibictn_label_departamento',
            description_msgid='ibictn_help_departamento',
            i18n_domain='ibictn',
        )
    ),

    TextField(
        name='observacoes',
        widget=TextAreaWidget(
            label="Observações",
            description="",
            label_msgid='ibictn_label_observacoes',
            description_msgid='ibictn_help_observacoes',
            i18n_domain='ibictn',
        )
    ),

    FileField(
        name='anexo',
        widget=FileWidget(
            label="Anexo",
            description="qualquer arquivo relevante",
            label_msgid='ibictn_label_anexo',
            description_msgid='ibictn_help_anexo',
            i18n_domain='ibictn',
        ),
        storage=AttributeStorage()
    ),

    StringField(
        name='aprovador',
        widget=SelectionWidget(
            label="Responsável por aprovar este documento",
            description="",
            label_msgid='ibictn_label_aprovador',
            description_msgid='ibictn_help_aprovador',
            i18n_domain='ibictn',
        ),
        required=1,
        vocabulary='getLista_aprovadores'
    ),

    StringField(
        name='provedor',
        widget=SelectionWidget(
            label="Responsável por prover este serviço",
            description="preenchido pelo aprovador",
            label_msgid='ibictn_label_provedor',
            description_msgid='ibictn_help_provedor',
            i18n_domain='ibictn',
        ),
        read_permission="ibictn: Ler campo",
        vocabulary='getLista_provedores',
        write_permission="ibictn: Escrever campo"
    ),

),
)

##code-section after-local-schema #fill in your manual code here
##/code-section after-local-schema

formulario_schema = schema.copy()

##code-section after-schema #fill in your manual code here
##/code-section after-schema

class formulario:
    """
    """
    security = ClassSecurityInfo()

    allowed_content_types = []

    actions =  (


       {'action': "string:${object_url}/properties",
        'category': "object",
        'id': 'metadata',
        'name': 'properties',
        'permissions': ("View",),
        'condition': 'python:False'
       },


    )

    _at_rename_after_creation = True

    schema = formulario_schema

    ##code-section class-header #fill in your manual code here
    ##/code-section class-header

    # Methods

    security.declarePublic('getLista_aprovadores')
    def getLista_aprovadores(self):
        """Retorna a lista de aprovadores"""
        portal_ibictn = getToolByName(self, 'portal_ibictn')
        result = []
        vocabulary = portal_ibictn.getVocab_aprovadores()

        for item in vocabulary:
            parts = item.split(';')
            result.append([parts[0], parts[1]])

        return DisplayList(tuple(result))

    security.declarePublic('sendMail')
    def sendMail(self, message='', toAddresses=None, subject = ''):
        """ Manda e-mail para o responsável"""

        fromEmail = "intranet@ibict.br"

        if toAddresses is None:
            log('[ibctn] Cannot send notification email: email sender addresses or name not set')
            return

        plone_utils = getToolByName(self, 'plone_utils')
        mailHost    = plone_utils.getMailHost()
        try:
            mailHost.send(message = message,
                          mto = toAddresses,
                          mfrom = fromEmail,
                          subject = subject)
        except:
            log('[ibictn] ERROR: MailHostError')
        log('[ibictn] MESSAGE: Mandou e-mail!')

    security.declarePublic('getLista_provedores')
    def getLista_provedores(self):
        """Retorna a lista de provedores de serviço"""
        portal_ibictn = getToolByName(self, 'portal_ibictn')
        result = []
        vocabulary = portal_ibictn.getVocab_provedores()

        for item in vocabulary:
            parts = item.split(';')
            result.append([parts[0], parts[1]])

        return DisplayList(tuple(result))

# end of class formulario

##code-section module-footer #fill in your manual code here
##/code-section module-footer



